package com.ys.controller;

import com.github.pagehelper.PageHelper;
import com.ys.mapper.DrguoqiMapper;
import com.ys.mapper.DruMapper;
import com.ys.mapper.DrugssMapper;
import com.ys.pojo.*;
import com.ys.service.*;
import com.ys.vo.PageVo;
import com.ys.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class JinhuoContorller {
    @Resource
    private DruMapper druMapper;
    @Resource
    private CgoderService cgoderService;
    @Resource
    private DrugsService drugsService;
    @Resource
    private DrugssService drugssService;
    @Resource
    private CgospeciService cgospeciService ;
    @Resource
    private CgoqualityService cgoqualityService ;


    @Resource
    private DrugssMapper drugssMapper ;
    @RequestMapping("/jinxingcaigou")
    @ResponseBody
//    shouhuojinhuo         shouhuojinhuo           shouhuojinhuo             shouhuojinhuo
    public ResultVo jinxingcaigou(String ids,String pageNo,String pageSize,String shihoujinhuo){
        Cgoder cgoder = new Cgoder();
        Subject subject  = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Administrators administrators = (Administrators) session.getAttribute("objs");
        cgoder.setPurchasId(administrators.getId());
        cgoder.setSupId(shihoujinhuo);
//        System.out.println(cgoder.getSupId()+"==00000000000000");
//        System.out.println(shihoujinhuo+"==11111111111111111");
        cgoder.setPurstate(0);
        cgoder.setPurjistate(Integer.parseInt(ids));
        cgoder.setComment("0");
        cgoder.setShenhecishu(1);
        cgoder.setDingdanStar(1);
        //添加采购单，添加完成获取采购单
        int caigoudanid =  cgoderService.adds(cgoder);

//        Cgoder cgoder1 = new Cgoder();
        //添加采购明细单
        List<Dru> list = druMapper.select();
        list.forEach(x->{
            Drugs drugs = drugsService.select(String.valueOf(x.getDrugsid()));
            Drugss drugss = new Drugss();
            drugss.setAbbreviation(drugs.getAbbreviation());
//            drugss.setBaozhiqi(drugs.get);
            drugss.setCategoryid(drugs.getCategoryid());
            drugss.setDrugsid(drugs.getDrugsid());
            drugss.setDrugsName(drugs.getDrugsName());
            drugss.setManufacture(drugs.getManufacture());
            drugss.setPrice(drugs.getPrice());
            drugss.setSpecifications(drugs.getSpecifications());
            drugss.setSupplierid(drugs.getSupplierid());
            drugss.setYongfa(drugs.getYongfa());
            drugss.setCompanyid(drugs.getCompanyid());
            int derderid = drugssService.addsinsert(drugss);
            Cgospeci cgospeci = new Cgospeci();
//            System.out.println(drugss.getId()+"商品的id");
//            System.out.println(cgoder.getCgoId()+"采购单的id");
            cgospeci.setCogId(drugss.getId());//采购单的id
            cgospeci.setDrugId(cgoder.getCgoId());// 药品的id
            cgospeci.setPurNumber(x.getNumberNumber());
            int i = cgospeciService.addsinsert(cgospeci);
//            System.out.println(i);
            int zongjiage = drugssMapper.selectjiage(String.valueOf(cgoder.getCgoId()));
            drugssMapper.uptatejiage(String.valueOf(zongjiage),String.valueOf(cgoder.getCgoId()));
        });
        ResultVo resultVo = new ResultVo();
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        resultVo.setObj(new PageVo(cgoderService.selectCgoder(ids,shihoujinhuo)));
        return resultVo;
    }

    @RequestMapping("/chaxunselectcaigoudingan")
    @ResponseBody
    public ResultVo chaxunselectcaigoudingan(String pageNo,String pageSize,String shihoujinhuo){
        ResultVo resultVo = new ResultVo();
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        resultVo.setObj(new PageVo(cgoderService.selectCgoder("0",shihoujinhuo)));
        return resultVo;
    }

    @RequestMapping("/zhuangeanye")
    public String zhuangeanye(String ids,Integer a){
       Subject subject = SecurityUtils.getSubject();
       Session session = subject.getSession();
       session.setAttribute("chakancaigouid",ids);
       session.setAttribute("asdd",a);
       return "redirect:zhuangeanyesss";
    }

    @RequestMapping("/zhuangeanyesss")
    public String zhuangeanyesss(){
        return "/jinhuo/insertyaossss";
    }


    @RequestMapping("/fanhuizhuangeanye")
    public String fanhuizhuangeanye(){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Integer a = (Integer) session.getAttribute("asdd");
        if(a==1){
            return "/jinhuo/insertyao";
        }

        if(a==2){
            return "/jinhuo/tuihuo";
        }

        if(a==3){
            return "/jinhuo/jintuichaxun";
        }else{
            return "/jinhuo/tuihuo";
        }
    }

    @RequestMapping("/genjuidjinxignshangpindecahxun")
    @ResponseBody
    public ResultVo genjuidjinxignshangpindecahxun(){
        ResultVo resultVo = new ResultVo();
            Subject subject = SecurityUtils.getSubject();
            Session session =subject.getSession();
            String ids = (String) session.getAttribute("chakancaigouid");
            resultVo.setObj(drugssService.list(ids));
        return resultVo;
    }


    @RequestMapping(value = "/zhijianyuanjiance", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo zhijianyuanjiance(@RequestBody Cgoquality cgoquality){
        ResultVo resultVo = new ResultVo();
        Session session = SecurityUtils.getSubject().getSession();
        Administrators administrators = (Administrators) session.getAttribute("objs");
        cgoquality.setQualPer(administrators.getId());
//        if(cgoquality.getQualState()==1){
//            cgoderService.updscomment(String.valueOf(cgoquality.getQualState()));
//        }
        if(cgoqualityService.addsinsert(cgoquality)>0){
            cgoderService.updsdate(String.valueOf(cgoquality.getCgoId()));
//            cgoderService.updscomment(String.valueOf(cgoquality.getCgoId()));
            resultVo.setMsg("检测成功!");
        }else {
            resultVo.setMsg("检测失败!");
        }
        return resultVo;
    }

    @RequestMapping(value = "/jiaoguoriqi", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo jiaoguoriqi(String ids){
        ResultVo resultVo = new ResultVo();
        if(cgoderService.updsdatessssssssss(ids)){
            resultVo.setMsg("交货成功!");
        }else{
            resultVo.setMsg("交货失败!");
        }
        return resultVo;
    }



    @RequestMapping(value = "/zaicishnehe", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo zaicishnehe(@RequestBody Cgoder cgoder){
        ResultVo resultVo = new ResultVo();
//        再次审核
        if(cgoderService.updsdss(String.valueOf(cgoder.getCgoId()))){
            Cgoquality cgoquality = new Cgoquality();
            cgoquality.setCgoId(cgoder.getCgoId());
            cgoquality.setQualState(Integer.parseInt(cgoder.getQualState()));
            cgoquality.setQualComment(cgoder.getQualComment());
            Session session = SecurityUtils.getSubject().getSession();
            Administrators administrators = (Administrators) session.getAttribute("objs");
            cgoquality.setQualMode(administrators.getId());
            if(Integer.parseInt(cgoder.getQualState())==1){
                cgoderService.updscomment(String.valueOf(cgoder.getCgoId()));
            }
            if(cgoqualityService.updsupdates(cgoquality)){

            resultVo.setMsg("再次审核成功!");
            }
        }else{
            resultVo.setMsg("再次审核失败!");
        }
        return resultVo;
    }

    @RequestMapping(value = "/tuihuossss",produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo tuihuossss(@RequestBody Cgoder cgoder){
        ResultVo resultVo = new ResultVo();
        if(cgoderService.updszhuangtai(String.valueOf(cgoder.getCgoId()))){
            resultVo.setMsg("已改为退货单!");
        }else{
            resultVo.setMsg("已改为退货单失败!");
        }
        return resultVo;
    }



    @Resource
    private DrguoqiMapper drguoqiMapper;
    @RequestMapping(value = "/jiyulingdaotijiao", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo jiyulingdaotijiao(@RequestBody Cgoder cgoder){
        ResultVo resultVo = new ResultVo();
        if(cgoderService.updscdingdanStar(String.valueOf(cgoder.getDingdanStar()),cgoder.getComment(),String.valueOf(cgoder.getCgoId()))){
            List<Drugss> list = drugssService.list(String.valueOf(cgoder.getCgoId()));
            //修改完数量
//            list.forEach(x->{
//                drugsService.updateku(Integer.parseInt(x.getPurNumber()),String.valueOf(x.getDrugsid()));
//            });
            //删除退货之后的货物
                list.forEach(x->{
                drguoqiMapper.deletes(String.valueOf(x.getDrugsid()));
            });

            resultVo.setMsg("提交成功!");
        }else{
            resultVo.setMsg("提交失败!");
        }
        return resultVo;
    }
//
//    @RequestMapping("/tuihuotuihuo")
//    @ResponseBody
//    public ResultVo tuihuotuihuo(@RequestBody Cgoder cgoder) {
//        ResultVo resultVo = new ResultVo();
//        try {
//            if (cgoderService.updscdingdanStar(String.valueOf(cgoder.getDingdanStar()), cgoder.getComment(), String.valueOf(cgoder.getCgoId()))) {
//                List<Drugss> list = drugssService.list(String.valueOf(cgoder.getCgoId()));
//                //修改完数量
//                list.forEach(x -> {
//                    drugsService.updateku(Integer.parseInt(x.getPurNumber()), String.valueOf(x.getDrugsid()));
//                });
//            }
//        } catch (Exception e) {
//            resultVo.setMsg("退货失败!");
//        }
//        resultVo.setMsg("退货成功!");
//        return resultVo;
//    }

    @RequestMapping(value = "/tuihuotuihuo", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultVo tuihuotuihuo(@RequestBody Cgoder cgoder) {
        ResultVo resultVo = new ResultVo();
        try {
            if (cgoderService.updscdingdanStar(String.valueOf(cgoder.getDingdanStar()), cgoder.getComment(), String.valueOf(cgoder.getCgoId()))) {
                List<Drugss> list = drugssService.list(String.valueOf(cgoder.getCgoId()));
                cgoqualityService.updQulaState(cgoder.getCgoId());
                //修改完数量
                list.forEach(x -> {
                    drugsService.updateku(Integer.parseInt(x.getPurNumber()), String.valueOf(x.getDrugsid()));
                });
            }
        } catch (Exception e) {
            resultVo.failure("退货失败!");
        }
        resultVo.success("退货成功!");
        return resultVo;
    }
}
